var $ = window.Zepto;
var root = window.player;
var controlmanager;
var audio = new root.audioManager();
//点击切割
var songList;
var index = 0;

function bindEvent() {
    $scope.on('play:change',function(e,index){
        audio.setAudioResource(songList[index].audio);
        if(audio.status == 'play'){
            audio.play();
            root.processor.start();
        }else{
            audio.pause();
            // root.processor.stop();
        }
        //渲染时间
        root.processor.renderAllTime(songList[index].duration)
    })
    $scope.on('click', '.prev-btn', function () {
        index = controlmanager.prev();
         if(audio.status == 'play'){
            root.processor.start(0);
         }else{
            root.processor.update(0);
         }
         
        //渲染页面
        root.render(songList[index]);
         //触发 播放音乐
         $scope.trigger("play:change",index);
    });

    $scope.on('click', '.next-btn', function () {
        index = controlmanager.next();
        if(audio.status == 'play'){
            root.processor.start(0);
         }else{
            root.processor.update(0);
         }
        //渲染页面
        root.render(songList[index]);
        // //触发 播放音乐
        $scope.trigger("play:change",index);
    });

    $scope.on('click','.play-btn',function(){
        if(audio.status == "play"){
            audio.pause();
            root.processor.stop();
        }else{
            audio.play();
            root.processor.start();
        }
        $(this).toggleClass("playing");
    })
}

function bindTouch(){
    var touchPonit = $scope.find(".slider-pointer");
    // var proWrapper =  $scope.find(".pro-bottom")[0];
    var offset = $scope.find(".pro-bottom").offset();
    var left = offset.left;
    var width = offset.width;//进度条总长
    //点当前的位置
    var pointOffsetLeft = $scope.find(".slider-pointer")[0].offsetLeft;
    touchPonit.on('touchstart',function(e){
       root.processor.stop();
    }).on('touchmove',function(e){
        var touch = e.changedTouches[0];
        var x = touch.clientX;
       var per =  (x - left)/width;
       if(per >=0 && per <= 1){
            root.processor.update(per);
       }
        
    }).on('touchend',function(e){
        var x = e.changedTouches[0].clientX;
        var per = (x-left)/width;
        if(per >= 0 && per <= 1){
            var duration = songList[index].duration * per;
            audio.playTo(duration);
            $scope.find('.play-btn').addClass("playing");
            audio.status = 'play';
        }
    });
}
function getData(url) {
    $.ajax({
        type: "GET",
        url: '../mock/data.json',
        success: function (data) {
            songList = data;
            controlmanager = new root.controlManager(songList.length);
            //渲染页面
            root.render(songList[index]);
            bindTouch();
            //触发 播放音乐
            $scope.trigger("play:change",index);
        },
        error: function () {
            console.log("error");
        }

    })
}
getData("../mock/data.json");

bindEvent();